package com.xyw.his00.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.xyw.his00.anno.OptionLog;
import com.xyw.his00.entity.ResponseData;
import com.xyw.his00.mapper.MedicalRecordMapper;
import com.xyw.his00.pojo.dbpojo.MedicalRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/medicalRecord/")
public class MedicalRecordController {
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Transactional
    @OptionLog
    @PostMapping("insertMedicalRecord")
    public ResponseData insertMedicalRecord(
            int registerId,
            String readme,
            String present,
            String presentTreat,
            String history,
            String allergy,
            String physique
    ) {
        // 查询是否存在对应registerId的病案记录
        MedicalRecord medicalRecord = medicalRecordMapper.selectOne(new QueryWrapper<MedicalRecord>().eq("register_id", registerId));

        // 如果不存在，则插入新记录
        if (medicalRecord == null) {
            int insertResult = medicalRecordMapper.insert(new MedicalRecord(registerId, readme, present, presentTreat, history, allergy, physique));
            if (insertResult > 0) {
                System.out.println("插入病案成功");
                return ResponseData.SUCCESS;
            } else {
                System.out.println("插入病案失败");
                return ResponseData.FAIL;
            }
        } else {
            // 如果存在，则更新记录
            UpdateWrapper<MedicalRecord> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("register_id", registerId)
                    .set("readme", readme)
                    .set("present", present)
                    .set("present_treat", presentTreat)
                    .set("history", history)
                    .set("allergy", allergy)
                    .set("physique", physique);
            int updateResult = medicalRecordMapper.update(null, updateWrapper); // 注意这里传递的第一个参数是null，或者使用具体的实体对象
            if (updateResult > 0) {
                System.out.println("更新病案成功");
                return ResponseData.SUCCESS;
            } else {
                System.out.println("更新病案失败");
                return ResponseData.FAIL;
            }
        }
    }


    @Transactional
    @OptionLog
    @PostMapping("updateMedicalRecord")
    public ResponseData updateMedicalRecord(
            int registerId,

            String proposal,
            String careful,
            String diagnosis,
            String cure
    ) {
        System.out.println("updateMedicalRecord");

        int res = medicalRecordMapper.updateMedicalRecord(registerId, proposal, careful, diagnosis, cure);
        if (res > 0) {
            return ResponseData.SUCCESS;
        } else {
            return ResponseData.FAIL;
        }
    }
}



